﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Business.Mst;
using ESOL.WinUI.Proj;


namespace ESOL.WinUI.Manufacture
{
    public partial class frmMaintainReworkOrder : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        private ProdPlanOrderEntity SelectedProdPlanOrder;
        private ProdPlanOrderItemEntity SelectedProdPlanOrderItem;
        private ProdPlanOrderItemEntity SelectedProdPlanOrderItemNew;
        #endregion

        #region ctors
        public frmMaintainReworkOrder()
        {
            InitializeComponent();
        }
        public frmMaintainReworkOrder(ProdPlanOrderItemEntity entity)
        {
            InitializeComponent();
            SelectedProdPlanOrderItem = entity;
        }

        #endregion

        #region functions
        private void SetCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbOrderItemStatus, "POI", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbOrderItemType, "POIT", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbOrderItemTypeNew, "POIT", true);

        }
        private void SetField()
        {
            if (SelectedProdPlanOrderItem != null)
            {
                SelectedProdPlanOrder = ProdPlanOrderBs.GetProdPlanOrderByID(SelectedProdPlanOrderItem.PlanOrderId);
                txtProductPlanOrder.Tag = SelectedProdPlanOrder;
                txtProductPlanOrder.Text = SelectedProdPlanOrder.PlanOrderCode;

                txtProductPlanOrderItem.Tag = SelectedProdPlanOrderItem;
                txtProductPlanOrderItem.Text = SelectedProdPlanOrderItem.OrderItemCode;
                dtpDeliveryDateOld.EditValue = SelectedProdPlanOrderItem.DeliveryDate;
                MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(SelectedProdPlanOrderItem.ProductId);
                txtProductCode.Text = mProduct.ProductCode;
                txtProductName.Text = mProduct.ProductName;
                txtMaterial.Text = mProduct.Material;
                txtDescription.Text = mProduct.Description;
                txtQty.Text = SelectedProdPlanOrderItem.Qty.ToString();
            }
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtQtyNew.Text))
            {
                MessageBox.Show("重工数量不可为空！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtQtyNew.Focus();
                return false;
            }
            if (dtpDeliveryDateNew.EditValue == null)
            {
                MessageBox.Show("计划交期不可为空！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                dtpDeliveryDateNew.Focus();
                return false;
            }
            if (cmbOrderItemType.SelectedValue == null)
            {
                MessageBox.Show("工令单类型不可为空！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cmbOrderItemType.Focus();
                return false;
            }
            return true;
        }

        private void SetEntity()
        {
            SelectedProdPlanOrderItemNew = new ProdPlanOrderItemEntity();
            SelectedProdPlanOrderItemNew.PlanOrderItemId = Guid.NewGuid().ToString();
            SelectedProdPlanOrderItemNew.PlanOrderId = SelectedProdPlanOrder.PlanOrderId;
            SelectedProdPlanOrderItemNew.ProductId = SelectedProdPlanOrderItem.ProductId;
            SelectedProdPlanOrderItemNew.BlueprintId = SelectedProdPlanOrderItem.BlueprintId;
            SelectedProdPlanOrderItemNew.TechnicPerson = SelectedProdPlanOrderItem.TechnicPerson;
            SelectedProdPlanOrderItemNew.Qty = Convert.ToDecimal(txtQtyNew.Text.ToString());
            SelectedProdPlanOrderItemNew.ProductType = SelectedProdPlanOrderItem.ProductType;
            SelectedProdPlanOrderItemNew.StorageQty = 0;
            SelectedProdPlanOrderItemNew.ProductedQty = 0;
            SelectedProdPlanOrderItemNew.DeliveryDate = (DateTime)dtpDeliveryDateNew.EditValue;
            SelectedProdPlanOrderItemNew.WorkorderCreateDate = DateTime.Now;
            SelectedProdPlanOrderItemNew.OrderType = cmbOrderItemTypeNew.SelectedValue.ToString();
            SelectedProdPlanOrderItemNew.RelatedPlanOrderItemId = SelectedProdPlanOrderItem.PlanOrderItemId;
            SelectedProdPlanOrderItemNew.Status = "POI01";
            SelectedProdPlanOrderItemNew.Remark = txtRemark.Text;
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            SetCombobox();
            SetField();
        }

        private void txtProductPlanOrder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductPlanOrderQuery frm = new frmMaintainProductPlanOrderQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanOrder != null)
            {
                txtProductPlanOrder.Tag = frm.SelectedProdPlanOrder;
                txtProductPlanOrder.Text = frm.SelectedProdPlanOrder.PlanOrderCode;
                SelectedProdPlanOrder = frm.SelectedProdPlanOrder;

                txtProductPlanOrderItem.Enabled = true;
                txtProductPlanOrderItem.ButtonClick+=new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(txtProductPlanOrderItem_ButtonClick);
            }
            else
            {

            }
        }

        private void txtProductPlanOrderItem_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainWorkingOrderQuery frm = new frmMaintainWorkingOrderQuery();
            frm.ProdPlanOrder = SelectedProdPlanOrder;
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProdPlanOrderItem != null)
            {
                txtProductPlanOrderItem.Tag = frm.SelectedProdPlanOrderItem;
                txtProductPlanOrderItem.Text = frm.SelectedProdPlanOrderItem.OrderItemCode;
                SelectedProdPlanOrderItem = frm.SelectedProdPlanOrderItem;

                if (SelectedProdPlanOrderItem == null)
                    return;
                dtpDeliveryDateOld.EditValue = SelectedProdPlanOrderItem.DeliveryDate;
                MstProductEntity mProduct = MstProductBs.GetMstProductTypeByID(SelectedProdPlanOrderItem.ProductId);
                txtProductCode.Text = mProduct.ProductCode;
                txtProductName.Text = mProduct.ProductName;
                txtMaterial.Text = mProduct.Material;
                txtDescription.Text = mProduct.Description;
                txtQty.Text = SelectedProdPlanOrderItem.Qty.ToString();
            }
        }

        protected override void Click_Save()
        {
            if (CheckData() == false)
                return;
            SetEntity();
            if (ProdPlanOrderItemBs.SaveReworkOrder(SelectedProdPlanOrderItemNew, SelectedProdPlanOrderItem) == true)
            {
                MessageBox.Show("保存成功！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                btnSetTechnic.Enabled = true;
                btnSave.Enabled = false;
            }
            else
            {
                MessageBox.Show("保存失败！", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnSetTechnic_Click(object sender, EventArgs e)
        {
            if (SelectedProdPlanOrderItemNew == null)
                return;
            frmMaintainManufactureTechnic frm = new frmMaintainManufactureTechnic(SelectedProdPlanOrderItemNew);
            frm.ShowDialog();
        }
        #endregion

    }
}
